package com.zoonref.viewbehavior;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.RecyclerView;

public class SampleTitleBehaviorHeadTitle extends CoordinatorLayout.Behavior<View> {
    // 列表顶部和title底部重合时，列表的滑动距离。
    private float deltaY;
    private float startY;//开始变得地方


    public SampleTitleBehaviorHeadTitle() {
    }

    public SampleTitleBehaviorHeadTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof RecyclerView;
    }

    //被观察的view发生改变时回调
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        ViewGroup appBarLayout = (ViewGroup) parent.getChildAt(0);
        ViewGroup collapsingToolbarLayout = (ViewGroup) appBarLayout.getChildAt(0);

        ViewGroup linearLayout = (ViewGroup) collapsingToolbarLayout.getChildAt(0);

        ViewGroup childAt1 = (ViewGroup) linearLayout.getChildAt(0);

        View childAt =  childAt1.getChildAt(0);

        int[] screenLocation = new int [2];
        childAt.getLocationOnScreen(screenLocation);
        startY=child.getHeight();
        if (screenLocation[1]>startY){
            child.setAlpha(0);
            return true;
        }
        if (screenLocation[1]>=0){
            child.setAlpha((startY-screenLocation[1])/startY);
        }else {
            child.setAlpha(1);

        }

        return true;
    }
}
